package com.igeek.shop.web.shouye;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.igeek.shop.entity.Category;
import com.igeek.shop.entity.PageBeanTwo;
import com.igeek.shop.entity.Product;
import com.igeek.shop.service.shouye.DetailsImpService;
import com.igeek.shop.service.shouye.IndexService;
import com.igeek.shop.service.shouye.IndexServiceImp;

public class IndexServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IndexService is = new IndexServiceImp();
	private DetailsImpService ds = new DetailsImpService();
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String state = req.getParameter("state");
		if(state.equals("pageInit")) {
			this.pageInit(req, resp);
		}else if(state.equals("initnav")) {
			this.initnav(req,resp);
		}else if(state.equals("categorypage")) {
			this.categorypage(req,resp);
		}
	}

	private void categorypage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		// TODO Auto-generated method stub
		String page = request.getParameter("page");
		int i = Integer.parseInt(page);
		//???该干什么了？？？
		String cid = request.getParameter("cid");
		//拿到该项共有多少条数据
		long productNumberByCid = is.getProductNumberByCid(cid);
		PageBeanTwo pageBeantwo = is.getPageBeanByCategory(i, 12, productNumberByCid, cid);
		request.setAttribute("pageBeantwo", pageBeantwo);
		
		
		//浏览记录
		Cookie[] cookies = request.getCookies();
		List<Product> historyList = new ArrayList<Product>();
		if(cookies!=null){
		for(Cookie cookie : cookies){
			if("pids".equals(cookie.getName())){
				String pids = cookie.getValue();
				String[] pids_arr = pids.split(",");
				for(String pid : pids_arr){
					Product product = ds.hotdetails(pid);
					historyList.add(product);
				}
			}
		}
		}
		
		request.setAttribute("historyList", historyList);
		request.getRequestDispatcher("/product_list.jsp").forward(request, response);
		
	}

	private void initnav(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		// TODO Auto-generated method stub
		//??查询所有的种类 。
		List<Category> categoryList = is. querylb();
		//Gson  ...
		Gson gson = new Gson();
		String json = gson.toJson(categoryList);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write(json);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void pageInit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		//System.out.println("。。。");
		//???
		PageBeanTwo pageBeantwo = is.getpageBean(1);
		//把封装完成的pageBean对象 传入到前台。
		request.setAttribute("pageBeantwo", pageBeantwo);
		request.getRequestDispatcher("/product_list.jsp").forward(request, response);
		System.out.println(pageBeantwo);
	}



}
